package com.forlove.app.qselect.index.presenter;

import com.forlove.app.ForloveApplication;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.DataManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.index.entity.FriendCircleInfoEntity;
import com.forlove.app.qselect.index.entity.PageEntity;
import com.forlove.app.qselect.index.service.IMineCircleInfoService;
import com.forlove.app.qselect.index.service.impl.MineCircleInfoServiceImpl;
import com.forlove.app.qselect.index.view.MineCircleInfoView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yjl on 2017/10/26.
 */

public class MineCircleInfoPresenter extends IBasePresenter {

    private MineCircleInfoView mineCircleInfoView;
    private IMineCircleInfoService mineCircleInfoService;
    private PageEntity pageEntity;
    private List<FriendCircleInfoEntity> infoEntities = new ArrayList<>();

    public MineCircleInfoPresenter(MineCircleInfoView mineCircleInfoView) {
        this.mineCircleInfoView = mineCircleInfoView;
        if (mineCircleInfoService == null) {
            mineCircleInfoService = new MineCircleInfoServiceImpl();
            setService(mineCircleInfoService);
        }
    }


    public synchronized void loadFriendInfoData(final PageEntity pageEntity) {
        this.pageEntity = pageEntity;
        Map param = new HashMap();
        param.put("type", pageEntity.mType);
        param.put("page", pageEntity.mPage);
        LogUtil.e("yjl", "page:" + pageEntity.mPage);
        mineCircleInfoService.loadData(ServerManager.ACTION_GET_INDEX_INFO, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "我的圈友信息：" + result);
                pageEntity.mPage = ParseHelper.getInteger(result, "data.page");
                List<FriendCircleInfoEntity> friendCircleInfoEntities =
                        ParseHelper.getModelList(result, "data.list", FriendCircleInfoEntity.class);
                if (pageEntity.mPage == 1 && friendCircleInfoEntities != null) {
                    infoEntities.clear();
                    infoEntities.addAll(friendCircleInfoEntities);
                    DataManager.saveMineCircleInfo(ForloveApplication.getInstance(), result);
                } else if (pageEntity.mPage > 1
                        && friendCircleInfoEntities != null
                        && friendCircleInfoEntities.size() > 0) {
                    infoEntities.addAll(friendCircleInfoEntities);
                } else if (friendCircleInfoEntities == null || friendCircleInfoEntities.size() == 0) {
                    if (pageEntity.mPage > 1) {
                        pageEntity.mPage--;
                    }
                }
                if (mineCircleInfoView != null) {
                    mineCircleInfoView.updateFriendCircleInfoView(pageEntity, infoEntities);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                mineCircleInfoView.showErrorData(result);
            }
        });

    }

    public synchronized void loadMoreFriendInfData() {
        if (this.pageEntity != null) {
            pageEntity.mPage++;
        }
        LogUtil.e("yjl", "loadMoreData:" + pageEntity.mPage);
        loadFriendInfoData(pageEntity);
    }
}
